$title  read_implan_state

* reads an implan dataset for one state and converts it to a sam readable by the
* remaining build stream. the input files export from implan should be in
* build/data/implan and have the notation xx.gms, where xx is the lower case two
* letter abbreviation for the state. the converted sam is stored in the file
* build/data/implan/xx.gdx.

* based on readall.gms from IMPLANinGAMS by Tom Rutherford



* ------------------------------------------------------------------------------
* load the implan sam data
* ------------------------------------------------------------------------------

* if a state abbreviation is not supplied then exit
$if not set state $exit

sets
  r                     rows and columns in the sam /
    1*536               industries
    3001*3536           commodities
    5001                employee compensation
    6001                proprietary income
    7001                other property income
    8001                indirect business taxes
    10001*10009         households
    11001               federal government nondefense
    11002               federal government defense
    11003               federal government investment
    12001               state local govt noneducation
    12002               state local govt education
    12003               state local govt investment
    13001               enterprises (corporations)
    14001               gross private fixed investment (capital)
    14002               inventory additions - deletions
    25001               foreign trade
    28001               domestic trade
  /

  t                     account codes  /
    3001*3536           commodities
    15001               corporate profits with iva
    15002               emp comp (wages,salary without soc sec)
    15003               employee comp (other labor income)
    15004               proprietors inc (without soc sec & cca)
    15005               rent with capital consumption adj
    15006               business transfers
    15007               dividends
    15008               interest (net-from industries)
    15009               interest (gross)
    15010               transfers
    15011               surplus or deficit
    15012               savings (surplus) not use
    15013               wage accruals less surplus
    15014               social ins tax-employee contribution
    15015               social ins tax-employer contribution
    15016               surplus-subsidy-govt enterprises
    15017               indirect bus tax: excise taxes
    15018               indirect bus tax: custom duty
    15019               indirect bus tax: fed nontaxes
    15020               indirect bus tax: sales tax
    15021               indirect bus tax: property tax
    15022               indirect bus tax: motor vehicle lic
    15023               indirect bus tax: severance tax
    15024               indirect bus tax: other taxes
    15025               indirect bus tax: s-l nontaxes
    15026               corporate profits tax
    15027               personal tax: income tax
    15028               personal tax: estate and gift tax
    15029               personal tax: nontaxes (fines-fees)
    15030               personal tax: motor vehicle license
    15031               personal tax: property taxes
    15032               personal tax: other tax (fish and hunt)
    15033               capital consumption allowance
    15034               retained profits (profits with iva&cca)
    15035               nipa statistical discrepency
    15036               interest (net-from row)
    15037               factor trade
    15038               adjustment to retained earnings
    15050               commodity use
    15051               commodity trade
    15052               commodity make
    15053               factor receipts
    15054               foreign commodity transshipments
    15055               industry use
    15056               industry trade
  /;

alias (r,c);

* load the implan data for the state
parameter
  sam(r,t,c)            base year social accounts /
$offlisting
$include build\data\implan\%state%.gms
$onlisting
  /;

*  extract non-zero domain
set
  samdomain(r,t,c);
$onuni
option samdomain < sam;
$offuni

* scale the data from million $ to billion $
sam(samdomain(r,t,c)) = sam(r,t,c)*(1/1000);



* ------------------------------------------------------------------------------
* check sam balance and fix any trade imbalances
* ------------------------------------------------------------------------------

parameter
  rowsum                 sam row sums
  colsum                 sam column sums
  check                  sam balance;

* row and column sums in the sam
rowsum(r) = sum(samdomain(r,t,c), sam(r,t,c));
colsum(c) = sum(samdomain(r,t,c), sam(r,t,c));

* correct any imbalances in the trade accounts through transfers from domestic
* to foreign trade accounts
if (rowsum("25001")>colsum("25001"),
  samdomain("28001","15010","25001") = yes;
  sam("28001","15010","25001") = sam("28001","15010","25001")
                                 +rowsum("25001")-colsum("25001");
else
  samdomain("25001","15010","28001") = yes;
  sam("25001","15010","28001") = sam("25001","15010","28001")
                                 +colsum("25001")-rowsum("25001");
);

* computed updated row and column sums in the sam
rowsum(r) = sum(samdomain(r,t,c), sam(r,t,c));
colsum(c) = sum(samdomain(r,t,c), sam(r,t,c));

* display the row/column balance in the sam
check(r,"rowsum") = rowsum(r);
check(r,"colsum") = colsum(r);
check(r,"tolerance") = round(rowsum(r)-colsum(r),5);
display check;

*  check subtotal accounts:
sets
  st                    subtotal accounts /
    industries
    commodities
    factor
    households
    fed
    state
    corp
    invest
    foreign
    domestic
  /

  stmap(st,r)           mapping from accounts to subtotal accounts /
    industries.(1*536)
    commodities.(3001*3536)
    factor.(5001,6001,7001,8001)
    households.(10001*10009)
    fed.(11001,11002,11003)
    state.(12001,12002,12003)
    corp.13001
    invest.(14001,14002)
    foreign.25001
    domestic.28001
  /;

alias (st,rt,ct);

parameter
  stsam                 subtotals of sam values;

* aggregate the sam entries into the subtotal accounts
stsam(rt,ct) = sum((r,c,t)$(stmap(rt,r) and stmap(ct,c)), sam(r,t,c));

* compute the row and colum totals and check the differences
stsam(rt,"total") = sum(ct, stsam(rt,ct));
stsam("total",ct) = sum(rt, stsam(rt,ct));
stsam("r-c diff",ct) = stsam(ct,"total") - stsam("total",ct);
display stsam;



* ------------------------------------------------------------------------------
* define the mapping from implan row/col and accounts
* ------------------------------------------------------------------------------

sets
  s(r)                  industries     / 1*536 /
  g(r)                  commodities    / 3001*3536 /
  f(r)                  factors        / 5001,6001,7001,8001 /
  i(r)                  institutions   / 10001*10009,11001*11003,12001*12003,
                                         13001,14001*14002 /
  trd(r)                trade          / 25001 foreign, 28001 domestic /

  j                     sam rows-cols /
*  goods and sectors
    (1*536)

*  factors
    empl                employee compensation (5001)
    prop                proprietary income (6001)
    othp                other property income (7001)
    btax                indirect business taxes (8001)

*  institutions
    hhl                 households lt15k (10001)
    hh15                households 15-30k (10002)
    hh30                households 30-40k (10003)
    hh40                households 40-50k (10004)
    hh50                households 50-70k (10005)
    hh70                households 70-100k (10006)
    hh100               households 100-150k (10007)
    hh150               households 150-200k (10008)
    hh200               households 200k+ (10009)
    fnd                 federal government nondefense (11001)
    fdf                 federal government defense (11002)
    fin                 federal government investment (11003)
    sln                 state local govt noneducation (12001)
    sle                 state local govt education (12002)
    sin                 state local govt investment (12003)
    ent                 enterprises (corporations) (13001)
    inv                 gross private fixed investment (capital) (14001)
    stk                 inventory additions deletions (14002)

*  trade flows
    ftrd                foreign trade (25001)
    dtrd                domestic trade (28001)
  /

  map(j,*)              maping from j space (acronyms) to r space (implan rows) /
    empl.5001           employee compensation
    prop.6001           proprietary income
    othp.7001           other property income
    btax.8001           indirect business taxes
    hhl.10001           households lt15k
    hh15.10002          households 15-30k
    hh30.10003          households 30-40k
    hh40.10004          households 40-50k
    hh50.10005          households 50-70k
    hh70.10006          households 70-100k
    hh100.10007         households 100-150k
    hh150.10008         households 150-200k
    hh200.10009         households 200k+
    fnd.11001           federal government nondefense
    fdf.11002           federal government defense
    fin.11003           federal government investment
    sln.12001           state local govt noneducation
    sle.12002           state local govt education
    sin.12003           state local govt investment
    ent.13001           enterprises (corporations)
    inv.14001           gross private fixed investment (capital)
    stk.14002           inventory additions deletions
    ftrd.25001          foreign trade
    dtrd.28001          domestic trade
  /

  implan(j)             implan sectors in j space    /1*536/;

alias (j,jj);

* add commodities to the mapping in accordance with the industry mapping
map(implan,implan) = yes;
loop((s,g)$(ord(g) eq ord(s)), map(j,g) = map(j,s););

* check the mapping to make sure each row of the implan sam is mapped to one of
* the new account labels and each of the new account labels is mapped to a
* row in the implam sam
set
  maperror(*)           identifies rows which are improperly mapped;
maperror(r) = yes$(sum(j$map(j,r), 1)-1);
abort$card(maperror) " type 1 error in mapping -- good not mapped:",maperror;
maperror(j) = yes$(sum(r$map(j,r), 1) eq 0);
abort$card(maperror) " type 2 error in mapping -- target not used:",maperror;

* define the core sets originally in r space in j space
set
  sj(j)                 industries in j space
  gj(j)                 commodities in j space
  fj(j)                 factors in j space
  ij(j)                 institutions in j space
  tj(j)                 trade in j space;

sj(j) = yes$sum(s$map(j,s), 1);
gj(j) = yes$sum(g$map(j,g), 1);
fj(j) = yes$sum(f$map(j,f), 1);
ij(j) = yes$sum(i$map(j,i), 1);
tj(j) = yes$sum(trd$map(j,trd), 1);

sets
  tt                    account acronyms /
    (1*536)             commodities
    cprf                corporate profits with iva (15001)
    ncmp                emp comp (wages and salary without soc sec) (15002)
    ecmp                employee comp (other labor income) (15003)
    prop                proprietors inc (without soc sec & cca) (15004)
    rent                rent with capital consumption adj (15005)
    btrn                business transfers (15006)
    divd                dividends (15007)
    nint                interest (net-from industries) (15008)
    gint                interest (gross) (15009)
    trns                transfers (15010)
    srpl                surplus or deficit (15011)
    save                savings (surplus) not use (15012)
    wage                wage accruals less surplus (15013)
    sstw                social ins tax-employee contribution (15014)
    sstf                social ins tax-employer contribution (15015)
    sgov                surplus-subsidy-govt enterprises (15016)
    excs                indirect bus tax: excise taxes (15017)
    duty                indirect bus tax: custom duty (15018)
    nont                indirect bus tax: fed nontaxes (15019)
    stax                indirect bus tax: sales tax (15020)
    bptx                indirect bus tax: property tax (15021)
    bmvt                indirect bus tax: motor vehicle lic (15022)
    sevt                indirect bus tax: severance tax (15023)
    otax                indirect bus tax: other taxes (15024)
    slnt                indirect bus tax: s-l nontaxes (15025)
    ctax                corporate profits tax (15026)
    pitx                personal tax: income tax (15027)
    egtx                personal tax: estate and gift tax (15028)
    fees                personal tax: nontaxes (fines-fees) (15029)
    pmvt                personal tax: motor vehicle license (15030)
    pptx                personal tax: property taxes (15031)
    fish                personal tax: other tax (fish and hunt) (15032)
    capc                capital consumption allowance (15033)
    retp                retained profits (profits with iva&cca) (15034)
    disc                nipa statistical discrepency (15035)
    fint                interest (net-from row) (15036)
    fact                factor trade (15037)
    radj                adjustment to retained earnings (15038)
    cuse                commodity use (15050)
    ctrd                commodity trade (15051)
    cmke                commodity make (15052)
    frpt                factor receipts (15053)
    ftrn                foreign commodity transshipments (15054)
    iuse                industry use (15055)
    itrd                industry trade (15056)
  /

  mapt(tt,t)            mapping from account codes to acronyms /
    cprf.15001          corporate profits with iva (15001)
    ncmp.15002          emp comp (wages and salary without soc sec) (15002)
    ecmp.15003          employee comp (other labor income) (15003)
    prop.15004          proprietors inc (without soc sec & cca) (15004)
    rent.15005          rent with capital consumption adj (15005)
    btrn.15006          business transfers (15006)
    divd.15007          dividends (15007)
    nint.15008          interest (net-from industries) (15008)
    gint.15009          interest (gross) (15009)
    trns.15010          transfers (15010)
    srpl.15011          surplus or deficit (15011)
    save.15012          savings (surplus)           not use (15012)
    wage.15013          wage accruals less surplus (15013)
    sstw.15014          social ins tax-employee contribution (15014)
    sstf.15015          social ins tax-employer contribution (15015)
    sgov.15016          surplus-subsidy-govt enterprises (15016)
    excs.15017          indirect bus tax: excise taxes (15017)
    duty.15018          indirect bus tax: custom duty (15018)
    nont.15019          indirect bus tax: fed nontaxes (15019)
    stax.15020          indirect bus tax: sales tax (15020)
    bptx.15021          indirect bus tax: property tax (15021)
    bmvt.15022          indirect bus tax: motor vehicle lic (15022)
    sevt.15023          indirect bus tax: severance tax (15023)
    otax.15024          indirect bus tax: other taxes (15024)
    slnt.15025          indirect bus tax: s-l nontaxes (15025)
    ctax.15026          corporate profits tax (15026)
    pitx.15027          personal tax: income tax (15027)
    egtx.15028          personal tax: estate and gift tax (15028)
    fees.15029          personal tax: nontaxes (fines-fees) (15029)
    pmvt.15030          personal tax: motor vehicle license (15030)
    pptx.15031          personal tax: property taxes (15031)
    fish.15032          personal tax: other tax (fish and hunt) (15032)
    capc.15033          capital consumption allowance (15033)
    retp.15034          retained profits (profits with iva&cca) (15034)
    disc.15035          nipa statistical discrepency (15035)
    fint.15036          interest (net-from row) (15036)
    fact.15037          factor trade (15037)
    radj.15038          adjustment to retained earnings (15038)
    cuse.15050          commodity use (15050)
    ctrd.15051          commodity trade (15051)
    cmke.15052          commodity make (15052)
    frpt.15053          factor receipts (15053)
    ftrn.15054          foreign commodity transshipments (15054)
    iuse.15055          industry use (15055)
    itrd.15056          industry trade (15056)
  /;

$onuni
mapt(tt,g)$(ord(g) = ord(tt)) = yes;
$offuni



* ------------------------------------------------------------------------------
* extract submatrices
* ------------------------------------------------------------------------------

parameter
  make(j,tt,jj)         domestic industry make matrix
  use(j,tt,jj)          domestic industry use matrix
  iuse(j,tt,jj)         domestic institutional use matrix
  fd(j,tt,jj)           factor input matrix : industry use of factors
  fexprt(j,tt,jj)       factor exports
  imake(j,tt,jj)        domestic institutional make matrix
  fs(j,tt,jj)           factor disbursement matrix
  trnsfer(j,tt,jj)      inter-institutional transfers
  fimprt(j,tt,jj)       factor imports
  trnshp(j,tt,jj)       transhipments
  sexport(j,tt,jj)      exports by sector
  iexport(j,tt,jj)      exports by institution
  simport(j,tt,jj)      imports by sector
  iimport(j,tt,jj)      imports by institution;

alias (i,ii), (ij,iij), (tj,ttj), (trd,ttrd);

set
  maps(s,j)             industry mapping from r space to j space
  mapg(g,j)             commodity mapping from r space to j space
  mapi(i,j)             institution mapping from r space to j space
  mapf(f,j)             factor mapping from r space to j space
  maptrd(trd,j)         trade mapping from r space to j space;

* define sub-mappings from r space to j space
maps(s,sj) = map(sj,s);
mapg(g,gj) = map(gj,g);
mapi(i,ij) = map(ij,i);
mapf(f,fj) = map(fj,f);
maptrd(trd,tj) = map(tj,trd);

* initialize space to track which of the implan sam records have been processed
set
  unread(r,t,c)         sam entries not yet processed;
unread(r,t,c) = samdomain(r,t,c);

* set up a tuple for more rapid execution
set
  jdomain(r,t,c,j,tt,jj);
jdomain(r,t,c,j,tt,jj) = no;

loop(samdomain(r,t,c),
  loop((j,tt,jj)$(map(j,r)*mapt(tt,t)*map(jj,c)),
    jdomain(r,t,c,j,tt,jj) = yes;
  )
);

* extract domestic industry make matrix
loop(samdomain(s,t,g),
  make(sj,tt,gj)$jdomain(s,t,g,sj,tt,gj) = sam(s,t,g);
);
unread(samdomain(s,t,g)) = no;

* extract domestic industry use matrix
loop(samdomain(g,t,s),
  use(gj,tt,sj)$jdomain(g,t,s,gj,tt,sj) = sam(g,t,s);
);
unread(samdomain(g,t,s)) = no;

* extract domestic institutional use matrix
loop(samdomain(g,t,i),
  iuse(gj,tt,ij)$jdomain(g,t,i,gj,tt,ij) = sam(g,t,i);
);
unread(samdomain(g,t,i)) = no;

* extract factor input matrix : industry use of factors
loop(samdomain(f,t,s),
  fd(fj,tt,sj)$jdomain(f,t,s,fj,tt,sj) = sam(f,t,s);
);
unread(samdomain(f,t,s)) = no;

* extract factor exports
loop(samdomain(f,t,trd),
  fexprt(fj,tt,tj)$jdomain(f,t,trd,fj,tt,tj) = sam(f,t,trd);
);
unread(samdomain(f,t,trd)) = no;

* extract domestic institutional make matrix
loop(samdomain(i,t,g),
  imake(ij,tt,gj)$jdomain(i,t,g,ij,tt,gj) = sam(i,t,g);
);
unread(samdomain(i,t,g)) = no;

* extract factor disbursement matrix
loop(samdomain(i,t,f),
  fs(ij,tt,fj)$jdomain(i,t,f,ij,tt,fj) = sam(i,t,f);
);
unread(samdomain(i,t,f)) = no;

* extract inter-institutional transfers
loop(samdomain(i,t,ii),
  trnsfer(ij,tt,iij)$jdomain(i,t,ii,ij,tt,iij) = sam(i,t,ii);
);
unread(samdomain(i,t,ii)) = no;

* extract factor imports
loop(samdomain(trd,t,f),
  fimprt(tj,tt,fj)$jdomain(trd,t,f,tj,tt,fj) = sam(trd,t,f);
);
unread(samdomain(trd,t,f)) = no;

* extract transhipments
loop(samdomain(trd,t,ttrd),
  trnshp(tj,tt,ttj)$jdomain(trd,t,ttrd,tj,tt,ttj) = sam(trd,t,ttrd);
);
unread(samdomain(trd,t,ttrd)) = no;

* extract exports by sector
loop(samdomain(s,t,trd),
  sexport(sj,tt,tj)$jdomain(s,t,trd,sj,tt,tj) = sam(s,t,trd);
);
unread(samdomain(s,t,trd)) = no;

* extract exports by institution
loop(samdomain(i,t,trd),
  iexport(ij,tt,tj)$jdomain(i,t,trd,ij,tt,tj) = sam(i,t,trd);
);
unread(samdomain(i,t,trd)) = no;

* extract imports by sector
loop(samdomain(trd,t,s),
  simport(tj,tt,sj)$jdomain(trd,t,s,tj,tt,sj) = sam(trd,t,s);
);
unread(samdomain(trd,t,s)) = no;

* extract imports by institution
loop(samdomain(trd,t,i),
  iimport(tj,tt,ij)$jdomain(trd,t,i,tj,tt,ij) = sam(trd,t,i);
);
unread(samdomain(trd,t,i)) = no;



* ------------------------------------------------------------------------------
* data consistency checks
* ------------------------------------------------------------------------------

parameter
  trace(*,*)            comparison of submatrix trace with full sam;

* sam submatrix totals
trace("total","data")   = sum((j,tt,jj), make(j,tt,jj)    + imake(j,tt,jj)   +
                                         sexport(j,tt,jj) + iexport(j,tt,jj) +
                                         simport(j,tt,jj) + iimport(j,tt,jj) +
                                         use(j,tt,jj)     + iuse(j,tt,jj)    +
                                         fd(j,tt,jj)      + fs(j,tt,jj)      +
                                         fexprt(j,tt,jj)  + fimprt(j,tt,jj)  +
                                         trnsfer(j,tt,jj) + trnshp(j,tt,jj));
trace("make","data")    = sum((j,tt,jj), make(j,tt,jj));
trace("use","data")     = sum((j,tt,jj), use(j,tt,jj));
trace("iuse","data")    = sum((j,tt,jj), iuse(j,tt,jj));
trace("fd","data")      = sum((j,tt,jj), fd(j,tt,jj));
trace("fexprt","data")  = sum((j,tt,jj), fexprt(j,tt,jj));
trace("imake","data")   = sum((j,tt,jj), imake(j,tt,jj));
trace("fs","data")      = sum((j,tt,jj), fs(j,tt,jj));
trace("trnsfer","data") = sum((j,tt,jj), trnsfer(j,tt,jj));
trace("fimprt","data")  = sum((j,tt,jj), fimprt(j,tt,jj));
trace("trnshp","data")  = sum((j,tt,jj), trnshp(j,tt,jj));
trace("sexport","data") = sum((j,tt,jj), sexport(j,tt,jj));
trace("iexport","data") = sum((j,tt,jj), iexport(j,tt,jj));
trace("simport","data") = sum((j,tt,jj), simport(j,tt,jj));
trace("iimport","data") = sum((j,tt,jj), iimport(j,tt,jj));

* implan data totals for submatrices
trace("total","sam")    = sum(samdomain(r,t,c), sam(r,t,c));
trace("make","sam")     = sum(samdomain(s,t,g), sam(s,t,g));
trace("use","sam")      = sum(samdomain(g,t,s), sam(g,t,s));
trace("iuse","sam")     = sum(samdomain(g,t,i), sam(g,t,i));
trace("fd","sam")       = sum(samdomain(f,t,s), sam(f,t,s));
trace("fexprt","sam")   = sum(samdomain(f,t,trd), sam(f,t,trd));
trace("imake","sam")    = sum(samdomain(i,t,g), sam(i,t,g));
trace("fs","sam")       = sum(samdomain(i,t,f), sam(i,t,f));
trace("trnsfer","sam")  = sum(samdomain(i,t,ii), sam(i,t,ii));
trace("fimprt","sam")   = sum(samdomain(trd,t,f), sam(trd,t,f));
trace("trnshp","sam")   = sum(samdomain(trd,t,ttrd), sam(trd,t,ttrd));
trace("sexport","sam")  = sum(samdomain(s,t,trd), sam(s,t,trd));
trace("iexport","sam")  = sum(samdomain(i,t,trd), sam(i,t,trd));
trace("simport","sam")  = sum(samdomain(trd,t,s), sam(trd,t,s));
trace("iimport","sam")  = sum(samdomain(trd,t,i), sam(trd,t,i));

display trace, unread;

parameter
  bmkchk                benchmark accounting checks;

* zero profit condition
bmkchk(sj,"profit") = round(sum(tt,  sum(gj, make(sj,tt,gj)-use(gj,tt,sj))
                                    +sum(tj, sexport(sj,tt,tj)-simport(tj,tt,sj))
                                    -sum(fj, fd(fj,tt,sj))), 6);

* domestic commodities market clearance condition
bmkchk(gj,"market") = round(sum(tt,  sum(sj, make(sj,tt,gj)-use(gj,tt,sj))
                                    +sum(ij, imake(ij,tt,gj)-iuse(gj,tt,ij))), 6);

* trade balance
bmkchk(tj,"trade") = round( sum((sj,tt), sexport(sj,tt,tj)-simport(tj,tt,sj))
                           +sum((fj,tt), fexprt(fj,tt,tj)-fimprt(tj,tt,fj))
                           +sum((ij,tt), iexport(ij,tt,tj)-iimport(tj,tt,ij))
                           +sum((ttj,tt), trnshp(ttj,tt,tj)-trnshp(tj,tt,ttj)), 6);

* factor market clearnace condition
bmkchk(fj,"market") =  round( sum((ij,tt), fs(ij,tt,fj))
                             +sum((tj,tt), fimprt(tj,tt,fj))
                             -sum((sj,tt), fd(fj,tt,sj))
                             -sum((tj,tt), fexprt(fj,tt,tj)), 6);

* budget constraint
bmkchk(ij,"income") = round( sum((gj,tt), imake(ij,tt,gj)-iuse(gj,tt,ij))
                            +sum((tj,tt), iexport(ij,tt,tj)-iimport(tj,tt,ij))
                            -sum((iij,tt), trnsfer(iij,tt,ij)-trnsfer(ij,tt,iij))
                            +sum((fj,tt), fs(ij,tt,fj)), 6);

display bmkchk;



* ------------------------------------------------------------------------------
* diagonalize the data set into commodity by commodity space
* ------------------------------------------------------------------------------

parameter
  supply                domestic production
  theta                 share of industry production devoted to commodity
  use_                  commodity sector by commodity output use matrix
  simport_              commodtiy sector import by sector matrix
  fd_                   commodity sector factor input matrix;

alias (gj,ggj);

$onuni

* domestic production
supply(sj) = sum((ggj,tt), make(sj,tt,ggj))+sum((ggj,tj), sexport(sj,ggj,tj));

* share of industry production devoted to commodity
theta(gj,sj)$supply(sj) = ( sum(tt, make(sj,tt,gj))
                           +sum(tj, sexport(sj,gj,tj)))/supply(sj);

$offuni

* convert production structure into a commodity rather than industry basis
use_(ggj,tt,gj)    = sum(sj, theta(gj,sj)*use(ggj,tt,sj));
simport_(tj,tt,gj) = sum(sj, theta(gj,sj)*simport(tj,tt,sj));
fd_(fj,tt,gj)      = sum(sj, theta(gj,sj)*fd(fj,tt,sj));



* ------------------------------------------------------------------------------
* save dataset
* ------------------------------------------------------------------------------

set
  h(j)                  private (household) institutions /
    hhl                 households lt15k (10001)
    hh15                households 15-30k (10002)
    hh30                households 30-40k (10003)
    hh40                households 40-50k (10004)
    hh50                households 50-70k (10005)
    hh70                households 70-100k (10006)
    hh100               households 100-150k (10007)
    hh150               households 150-200k (10008)
    hh200               households 200k+ (10009)
  /

  pub(j)                public (government) institutions /
    fnd                 federal government nondefense (11001)
    fdf                 federal government defense (11002)
    fin                 federal government investment (11003)
    sln                 state local govt noneducation (12001)
    sle                 state local govt education (12002)
    sin                 state local govt investment (12003)
  /

  corp(j)               corporate institutions /
    ent                 enterprises (corporations) (13001)
    inv                 gross private fixed investment (capital) (14001)
    stk                 inventory sales deletions (14002)
  /;

execute_unload 'build\data\implan\%state%.gdx', fj=f,tt=t,ij=i,j,gj=g,h,pub,
                                                corp,make,use_=use,iuse,fd_=fd,
                                                fexprt,imake,fs,trnsfer,fimprt,
                                                trnshp,sexport,iexport,
                                                simport_=simport,iimport;
